package com.efast.dmom.mgmt.service.mes.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.mes.LotRepairDto;
import com.efast.dmom.mgmt.bean.mes.ProductExcuteDto;
import com.efast.dmom.mgmt.dao.mes.TwBasLotRepairDao;
import com.efast.dmom.mgmt.entity.mes.TwBasLot;
import com.efast.dmom.mgmt.entity.mes.TwBasLotRepair;
import com.efast.dmom.mgmt.service.mes.TwBasLotRepairService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TwBasLotRepairServiceImpl implements TwBasLotRepairService{
    @Autowired
    private TwBasLotRepairDao twBasLotRepairDao;
    @Override
    public long getTwBasLotRepairRowCount(Assist assist){
        return twBasLotRepairDao.getTwBasLotRepairRowCount(assist);
    }
    @Override
    public List<TwBasLotRepair> selectTwBasLotRepair(Assist assist){
        return twBasLotRepairDao.selectTwBasLotRepair(assist);
    }
    @Override
    public TwBasLotRepair selectTwBasLotRepairByObj(TwBasLotRepair obj){
        return twBasLotRepairDao.selectTwBasLotRepairByObj(obj);
    }
    @Override
    public TwBasLotRepair selectTwBasLotRepairById(Integer id){
        return twBasLotRepairDao.selectTwBasLotRepairById(id);
    }
    @Override
    public int insertTwBasLotRepair(TwBasLotRepair value){
        return twBasLotRepairDao.insertTwBasLotRepair(value);
    }
    @Override
    public int insertNonEmptyTwBasLotRepair(TwBasLotRepair value){
        return twBasLotRepairDao.insertNonEmptyTwBasLotRepair(value);
    }
    @Override
    public int insertTwBasLotRepairByBatch(List<TwBasLotRepair> value){
        return twBasLotRepairDao.insertTwBasLotRepairByBatch(value);
    }
    @Override
    public int deleteTwBasLotRepairById(Integer id){
        return twBasLotRepairDao.deleteTwBasLotRepairById(id);
    }
    @Override
    public int deleteTwBasLotRepair(Assist assist){
        return twBasLotRepairDao.deleteTwBasLotRepair(assist);
    }
    @Override
    public int updateTwBasLotRepairById(TwBasLotRepair enti){
        return twBasLotRepairDao.updateTwBasLotRepairById(enti);
    }
    @Override
    public int updateTwBasLotRepair(TwBasLotRepair value, Assist assist){
        return twBasLotRepairDao.updateTwBasLotRepair(value,assist);
    }
    @Override
    public int updateNonEmptyTwBasLotRepairById(TwBasLotRepair enti){
        return twBasLotRepairDao.updateNonEmptyTwBasLotRepairById(enti);
    }
    @Override
    public int updateNonEmptyTwBasLotRepair(TwBasLotRepair value, Assist assist){
        return twBasLotRepairDao.updateNonEmptyTwBasLotRepair(value,assist);
    }

    public TwBasLotRepairDao getTwBasLotRepairDao() {
        return this.twBasLotRepairDao;
    }

    public void setTwBasLotRepairDao(TwBasLotRepairDao twBasLotRepairDao) {
        this.twBasLotRepairDao = twBasLotRepairDao;
    }
	@Override
	public void updateLotRepairByBatch(List<TwBasLotRepair> lotRepList, ProductExcuteDto dto) {
		// 批量更新指定工序指定LOT的维修数据
		String ids = "(";
		for(TwBasLotRepair lotRep: lotRepList) {
			ids+=lotRep.getRepairResultId()+","; 
		}
		ids = ids.substring(0,ids.length()-1)+")";
		Assist assist = new Assist();
		assist.setRequires(Assist.customRequire("repair_result_id in ", ids));
		twBasLotRepairDao.deleteTwBasLotRepair(assist);
		this.saveLotRepair(dto);
	}
	@Override
	public void saveLotRepair(ProductExcuteDto dto) {
		// 保存指定工序指定LOT的维修数据
		List<TwBasLotRepair> lotRepList = new ArrayList<TwBasLotRepair>();
		TwBasLot lot = dto.getRowData();
		for(LotRepairDto lotrep:dto.getRepair()) {
			TwBasLotRepair rep = new TwBasLotRepair();
			String[] defect = lotrep.getDefectCode().split("_");
			String[] repCode = lotrep.getRepairCode().split("_");
			if(defect.length>=2&&repCode.length>=2) {
                rep.setLotId(lot.getLotId());
                rep.setLotName(lot.getLotName());
                rep.setDefectCodeId(Integer.valueOf(defect[0]));
                rep.setDefectCode(defect[1]);
                rep.setDefectOwner(lotrep.getDefectOwner());
                rep.setModifyPartSn(lotrep.getModifyPartSn());
                rep.setModifyPartType(lotrep.getModifyPartType());
                rep.setPartSnBefore(lotrep.getPartSnBefore());
                rep.setTestResult(lotrep.getTestResult());
                rep.setProductionLine(lot.getProductionLine());
                rep.setOperationId(lot.getOperationId());
                rep.setOperation(lot.getOperation());
                rep.setRepairCodeId(Integer.valueOf(repCode[0]));
                rep.setRepairCode(repCode[1]);
                rep.setRoute(lot.getRoute());
                rep.setStation(lot.getStationName());
                rep.setStationId(lot.getStationId());
                rep.setProductCfgItemId(Integer.valueOf(repCode[0]));
                rep.setCreateTime(new Date());
                rep.setCreateUser(SpringWebUtils.getRequestUser());
                rep.setCompanyCode(SpringWebUtils.getRequestCompany());
                lotRepList.add(rep);
            }
		}
		if(lotRepList!=null&&lotRepList.size()>0) {
            twBasLotRepairDao.insertTwBasLotRepairByBatch(lotRepList);
        }
	}

}